# coding: utf-8
import json

import main


def signup(email,passwd,vrcode):
    #注册和修改密码
    #temp_dict = {email:{"passwd":passwd}}
    with open("user_data.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if email in old_data:
            if vrcode==old_data[email]['vrcode']:
                #old_data.update(temp_dict)
                old_data[email]['passwd'] = passwd
                with open("user_data.json", "w", encoding="utf-8") as f:
                    js_data = json.dumps(old_data, indent=4, separators=(',', ':')).encode('utf-8').decode(
                        'raw_unicode_escape')
                    f.write(js_data)
                print("registration success")
                data = {"success": "注册成功"}
                main.callback(data)
            else:
                print(vrcode+"验证码错误")
                data={"error":"验证码错误"}
                main.callback(data)
        else:
            data = {"error": "请先验证邮箱"}
            main.callback(data)


def pre_signup(email,vrcode):
    #预写入资料
    temp_dict = {email:{"passwd":"","name":"未设置","profile":"https://xn--15t.top/img/toux.jpg","vrcode":vrcode}}
    with open("user_data.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if email in old_data:
            old_data[email]['vrcode']=vrcode
        else:
            old_data.update(temp_dict)
    with open("user_data.json", "w", encoding="utf-8") as f:
        js_data = json.dumps(old_data, indent=4, separators=(',', ':')).encode('utf-8').decode('raw_unicode_escape')
        f.write(js_data)
    print("registration success")
    #data = {"success": "pre_ignup success"}
    #main.callback(data)

def del_account(email,passwd):
    with open("user_data.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if email in old_data:
            if passwd==old_data[email]['passwd']:
                try:
                    del old_data[email]
                    with open("user_data.json", "w", encoding="utf-8") as f:
                        js_data = json.dumps(old_data, indent=4, separators=(',', ':')).encode('utf-8').decode('raw_unicode_escape')
                        f.write(js_data)
                    print("销号成功")
                    main.callback("del success")
                except:
                    print("删除失败")
                    main.callback("del faild")
            else:
                print("密码错误")
                main.callback("error passwd")
        else:
            print( "未注册")
            main.callback("未注册")


def signin(email,passwd):
    with open("user_data.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if email in old_data:
            if passwd==old_data[email]['passwd']:
                print("登陆成功")
                data = {"success": "登陆成功"}
                main.callback(data)
            else:
                print("密码错误")
                data = {"error": "密码错误"}
                main.callback(data)
        else:
            print( "未注册")
            data = {"error": "未注册"}
            main.callback(data)

def personalize(email,passwd,name,profile):
    # 修改name和头像
    with open("user_data.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if email in old_data:
            if passwd==old_data[email]['passwd']:
                print("修改数据")
                old_data[email]['name']=name;
                old_data[email]['profile'] = profile;
                with open("user_data.json", "w", encoding="utf-8") as f:
                    js_data = json.dumps(old_data, indent=4, separators=(',', ':')).encode('utf-8').decode(
                        'raw_unicode_escape')
                    f.write(js_data)
                data = {"success": "修改成功"}
                main.callback(data)
            else:
                print("密码错误")
                data = {"error": "修改失败-密码错误"}
                main.callback(data)
        else:
            print( "未注册")
            data = {"error": "修改失败-未注册"+email}
            main.callback(data)

def get_userdata(email,passwd):
    with open("user_data.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if email in old_data:
            if passwd==old_data[email]['passwd']:
                print("返回数据")
                data = {"name": old_data[email]['name'],"profile":old_data[email]['profile'],"success":"验证通过-返回数据"}
                main.callback(data)
            else:
                print("密码错误")
                data = {"error": "验证失败-密码错误"}
                main.callback(data)
        else:
            print( "未注册")
            data = {"error": "验证失败-未注册"+email}
            main.callback(data)



#signup("1074637681@qq.com","xyxywan")
#signup("10afasd81@yu.com","xssssswan")
#signin("1074637681@qq.com","xyxywasdas")
#signin("1074637asaffsdfsdgqq.com","xyxywan")
#signin("1074637681@qq.com","xyxywan")
#del_account("1074637681@qq.com","xyxywan")
